package cn.janus.component;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.core.MessagePostProcessor;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class RocketMQProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendMessage(String topic, Object message) {
        rocketMQTemplate.convertAndSend(topic, message);
    }

    public void sendMessageAsync(String topic, String message, String tag) {
        rocketMQTemplate.asyncSend(topic, MessageBuilder.withPayload(message).build(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                // 成功发送后处理结果
                System.out.println("Message sent successfully. MsgId: " + sendResult.getMsgId());
            }

            @Override
            public void onException(Throwable e) {
                // 处理异常情况
                System.err.println("Failed to send message: " + e.getMessage());
            }
        });

    }
}